package com.sikaryofficial.information.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sikaryofficial.information.config.HISMKInformationProperties;
import com.sikaryofficial.information.domain.dto.req.ArticleExposureCountReq;
import com.sikaryofficial.information.domain.entity.ArticleExposureCount;
import com.sikaryofficial.information.domain.mapping.ArticleExposureCountMapping;
import com.sikaryofficial.information.service.IArticleExposureCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc :  文章数据同步管理类
 * @date : 2024/03/13 11:50
 */
@Component
@Slf4j
public class ArticleExposureManager {
    @Autowired
    private HISMKInformationProperties brandTenantProperties;
    @Autowired
    private IArticleExposureCountService exposureService;

    private  Date getDateOnlyYYYYMMDD() {
        // 假设你有一个Date对象
        Date originalDate = new Date();
        // 使用Calendar去除时分秒
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originalDate);
        // 清除时、分、秒、毫秒
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取只保留年月日的Date对象
        return calendar.getTime();
    }

    /**
     * 统计数据保存
     *
     * @param req
     * @return
     */
    @Async("asyncTaskExecutor")
    public Boolean saveData(ArticleExposureCountReq req) {
        if (req.getObjectId() <= 0L) {
            log.error("ObjectId 参数异常：{}", req.getObjectId());
            return false;
        }
        Integer tenantId = brandTenantProperties.getTenantByBrand(req.getBrandNo());
        ArticleExposureCount saveExposureCount = ArticleExposureCountMapping.INSTANCE.coverToEntity(req);
        saveExposureCount.setTenantId(tenantId);
        // 统计时间
        Date genDate = getDateOnlyYYYYMMDD();
        LambdaQueryWrapper<ArticleExposureCount> queryWrapper = new LambdaQueryWrapper<ArticleExposureCount>()
                .eq(ArticleExposureCount::getObjectId, req.getObjectId())
                .eq(ArticleExposureCount::getDeletedVersion, 0L)
                .eq(ArticleExposureCount::getGenDate, genDate);
        if (Objects.isNull(tenantId)) {
            queryWrapper.isNull(ArticleExposureCount::getTenantId);
        } else {
            queryWrapper.eq(ArticleExposureCount::getTenantId, tenantId);
        }
        queryWrapper.last(" limit 1 ");
        ArticleExposureCount curExposureCount = exposureService.getOne(queryWrapper);
        if (Objects.isNull(curExposureCount)) {
            // 新增
            saveExposureCount.setArticleExposureId(IdWorker.getId());
            saveExposureCount.setExposureCount(1L);
            saveExposureCount.setCreatedBy(0L);
            saveExposureCount.setCreatedTime(new Date());
            // 增加统计时间
            saveExposureCount.setGenDate(genDate);
            exposureService.save(saveExposureCount);
        } else {
            // 更新
            curExposureCount.setUpdatedBy(0L);
            curExposureCount.setUpdatedTime(new Date());
            Long exposureCount = curExposureCount.getExposureCount();
            curExposureCount.setExposureCount(Objects.isNull(exposureCount) ? 0L : exposureCount + 1);
            exposureService.updateById(curExposureCount);
        }
        return true;
    }
}
